package com.top.mall.main.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.base.BaseViewModel
import com.top.mall.custom.setTransitionStatus
import com.top.mall.main.mine.adapter.Area1Adapter
import com.top.mall.main.mine.adapter.Area2Adapter
import com.top.mall.main.mine.adapter.Area3Adapter
import kotlinx.android.synthetic.main.activity_area_list.*
import java.lang.StringBuilder

class AreaListActivity : OwnerViewModeActivity<BaseViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, AreaListActivity::class.java)
            activity.startActivityForResult(intent, 1)
        }
    }

    private val mArea1Adapter = Area1Adapter()

    private val mArea2Adapter = Area2Adapter()

    private val mArea3Adapter = Area3Adapter()

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_area_list

    override fun stateBar(): StatusBar {
        return StatusBar(view = flAreaListBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }
        rvAreaList1Body.adapter = mArea1Adapter.apply {
            setOnItemClickListener { _, _, position ->
                selectPosition = position
                val itemData = getItem(position)
                viewModel.userArea(province = itemData.id, grade = 2)

            }
        }
        rvAreaList2Body.adapter = mArea2Adapter.apply {
            setOnItemClickListener { _, _, position ->
                selectPosition = position
                val itemData = getItem(position)
                viewModel.userArea(city = itemData.id, grade = 3)
            }
        }
        rvAreaList3Body.adapter = mArea3Adapter.apply {
            setOnItemClickListener { _, _, position ->
                selectPosition = position
            }
        }

        tvAreaListKeep.setOnClickListener {
            if (mArea1Adapter.selectPosition == -1) {
                return@setOnClickListener
            }
            if (mArea2Adapter.selectPosition == -1) {
                return@setOnClickListener
            }
            if (mArea3Adapter.selectPosition == -1) {
                return@setOnClickListener
            }
            val data1 = mArea1Adapter.data[mArea1Adapter.selectPosition]
            val data2 = mArea2Adapter.data[mArea2Adapter.selectPosition]
            val data3 = mArea3Adapter.data[mArea3Adapter.selectPosition]
            val area = StringBuilder()
                .append(data1.name).append("\t")
                .append(data2.name).append("\t")
                .append(data3.name)
            val intent = Intent().apply {
                putExtra("result", area.toString())
                putExtra("province",data1.id)
                putExtra("city",data2.id)
                putExtra("district",data3.id)
            }
            setResult(1, intent)
            finish()
        }
    }

    override fun initData() {
        viewModel.userArea(grade = 1)
    }

    override fun initViewMode() {
        viewModel.userAreaLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            if (data.grade == 1) {
                mArea1Adapter.setNewInstance(data.data)
            } else if (data.grade == 2) {
                mArea2Adapter.data.clear()
                mArea2Adapter.selectPosition = -1
                mArea3Adapter.data.clear()
                mArea3Adapter.selectPosition = -1
                mArea2Adapter.setNewInstance(data.data)
            } else if (data.grade == 3) {
                mArea3Adapter.data.clear()
                mArea3Adapter.notifyDataSetChanged()
                mArea3Adapter.setNewInstance(data.data)
            }
        })
    }


}